package io.xccit.dao;

import io.xccit.pojo.Employee;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author CH_ywx
 * @Date 2023-04-02
 * @Description 员工Dao类
 */
@Repository
public class EmployeeDao {
    private Map<Integer, Employee> employeeMap;
    {
        employeeMap = new HashMap<Integer, Employee>();
        employeeMap.put(1001,new Employee(1001,"admin0",0,"admin0@163.com"));
        employeeMap.put(1002,new Employee(1002,"admin1",1,"admin1@163.com"));
        employeeMap.put(1003,new Employee(1003,"admin2",1,"admin2@163.com"));
        employeeMap.put(1004,new Employee(1004,"admin3",0,"admin3@163.com"));
        employeeMap.put(1005,new Employee(1005,"admin4",0,"admin4@163.com"));
    }
    private static Integer initID = 1006;//初始化ID

    /**
     * 模拟添加
     * @param employee 员工对象
     */
    public void saveEmployee(Employee employee){
        if (employee.getId() == null){
            employee.setId(initID++);
        }
        employeeMap.put(employee.getId(), employee);
    }

    /**
     * 模拟获取所有员工
     * @return 员工信息
     */
    public Collection<Employee> getAllEmployees(){
        System.out.println(employeeMap.values());
        return employeeMap.values();
    }

    /**
     * 模拟获取单个员工
     * @param id 员工id
     * @return 员工信息
     */
    public Employee getByID(@PathVariable Integer id){
        return employeeMap.get(id);
    }
    public void deleteByID(@PathVariable Integer id){
        Employee remove = employeeMap.remove(id);
        if (remove != null){
            System.out.println("删除成功!");
        }
    }

}
